home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / checkbox / registries / udev.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  14.1 KB  |  312 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import string
  7. import posixpath
  8. from curses.ascii import isprint
  9. from checkbox.lib.bit import get_bitmask, test_bit
  10. from checkbox.lib.cache import cache
  11. from checkbox.lib.dmi import Dmi, DmiNotAvailable
  12. from checkbox.lib.input import Input
  13. from checkbox.lib.pci import Pci
  14. from checkbox.lib.usb import Usb
  15. from checkbox.properties import String
  16. from checkbox.registry import Registry
  17. from checkbox.registries.command import CommandRegistry
  18. from checkbox.registries.link import LinkRegistry
  19. from checkbox.registries.map import MapRegistry
  20.  
  21. class DeviceRegistry(Registry):
  22.     
  23.     def __init__(self, environment, attributes):
  24.         super(DeviceRegistry, self).__init__()
  25.         self._environment = environment
  26.         self._attributes = attributes
  27.  
  28.     
  29.     def __str__(self):
  30.         strings = _[1]
  31.         return '\n'.join(strings)
  32.  
  33.     
  34.     def _get_bus(self):
  35.         sys_path = posixpath.join('/sys%s' % self._environment['DEVPATH'], 'subsystem')
  36.         if posixpath.islink(sys_path):
  37.             link = os.readlink(sys_path)
  38.             if '/' in link:
  39.                 return posixpath.basename(link)
  40.         
  41.  
  42.     
  43.     def _get_category(self):
  44.         if 'sys_vendor' in self._attributes:
  45.             return 'SYSTEM'
  46.         if 'IFINDEX' in self._environment:
  47.             return 'NETWORK'
  48.         if 'PCI_CLASS' in self._environment:
  49.             pci_class = self._environment['PCI_CLASS']
  50.             subclass_id = int(pci_class[-2:], 16)
  51.             class_id = int(pci_class[:-2], 16)
  52.             if class_id == Pci.BASE_CLASS_NETWORK:
  53.                 return 'NETWORK'
  54.             if class_id == Pci.BASE_CLASS_DISPLAY:
  55.                 return 'VIDEO'
  56.             if class_id == Pci.BASE_CLASS_SERIAL and subclass_id == Pci.CLASS_SERIAL_USB:
  57.                 return 'USB'
  58.             if class_id == Pci.BASE_CLASS_COMMUNICATION and subclass_id == Pci.CLASS_COMMUNICATION_MODEM:
  59.                 return 'MODEM'
  60.             if class_id == Pci.BASE_CLASS_INPUT and subclass_id == Pci.CLASS_INPUT_SCANNER:
  61.                 return 'SCANNER'
  62.             if class_id == Pci.BASE_CLASS_SERIAL and subclass_id == Pci.CLASS_SERIAL_FIREWIRE:
  63.                 return 'FIREWIRE'
  64.         if self._get_product_id():
  65.             return 'OTHER'
  66.  
  67.     
  68.     def _get_driver(self):
  69.         if 'DRIVER' in self._environment:
  70.             return self._environment['DRIVER']
  71.         if 'ID_USB_DRIVER' in self._environment:
  72.             return self._environment['ID_USB_DRIVER']
  73.  
  74.     
  75.     def _get_path(self):
  76.         return self._environment.get('DEVPATH')
  77.  
  78.     
  79.     def _get_product_id(self):
  80.         if 'PCI_ID' in self._environment:
  81.             (vendor_id, product_id) = self._environment['PCI_ID'].split(':')
  82.             return int(product_id, 16)
  83.         if 'PRODUCT' in self._environment and self._environment.get('DEVTYPE') == 'usb_interface':
  84.             (vendor_id, product_id, revision) = self._environment['PRODUCT'].split('/')
  85.             return int(product_id, 16)
  86.         for attribute in ('idProduct', 'model_id'):
  87.             if attribute in self._attributes:
  88.                 return int(self._attributes[attribute], 16)
  89.         
  90.  
  91.     
  92.     def _get_vendor_id(self):
  93.         if 'PCI_ID' in self._environment:
  94.             (vendor_id, product_id) = self._environment['PCI_ID'].split(':')
  95.             return int(vendor_id, 16)
  96.         if 'PRODUCT' in self._environment and self._environment.get('DEVTYPE') == 'usb_interface':
  97.             (vendor_id, product_id, revision) = self._environment['PRODUCT'].split('/')
  98.             return int(vendor_id, 16)
  99.         if 'idVendor' in self._attributes:
  100.             return int(self._attributes['idVendor'], 16)
  101.         vendor_id_path = posixpath.join(self._get_path(), '../vendor_id')
  102.         if posixpath.exists(vendor_id_path):
  103.             vendor_id = open(vendor_id_path, 'r').read().strip()
  104.             return int(vendor_id, 16)
  105.  
  106.     
  107.     def _get_subproduct_id(self):
  108.         if 'PCI_SUBSYS_ID' in self._environment:
  109.             pci_subsys_id = self._environment['PCI_SUBSYS_ID']
  110.             (subvendor_id, subproduct_id) = pci_subsys_id.split(':')
  111.             return int(subproduct_id, 16)
  112.  
  113.     
  114.     def _get_subvendor_id(self):
  115.         if 'PCI_SUBSYS_ID' in self._environment:
  116.             pci_subsys_id = self._environment['PCI_SUBSYS_ID']
  117.             (subvendor_id, subproduct_id) = pci_subsys_id.split(':')
  118.             return int(subvendor_id, 16)
  119.  
  120.     
  121.     def _get_product(self):
  122.         for element in ('NAME', 'RFKILL_NAME', 'POWER_SUPPLY_MODEL_NAME'):
  123.             if element in self._environment:
  124.                 return self._environment[element].strip('"')
  125.         
  126.         for attribute in ('description', 'model_name_kv', 'model', 'product_name'):
  127.             if attribute in self._attributes:
  128.                 return self._attributes[attribute]
  129.         
  130.         bus = self._get_bus()
  131.         if bus == 'sound':
  132.             device = posixpath.basename(self._environment['DEVPATH'])
  133.             match = re.match('(card|controlC|hwC|midiC)(?P<card>\\d+)', device)
  134.             if match:
  135.                 card = match.group('card')
  136.                 in_card = False
  137.                 file = open('/proc/asound/cards', 'r')
  138.                 for line in file.readlines():
  139.                     line = line.strip()
  140.                     match = re.match('(?P<card>\\d+) \\[', line)
  141.                     if in_card:
  142.                         match = re.match('(?P<name>.*) at (?P<address>0x[%s]{8}) irq (?P<irq>\\d+)' % string.hexdigits, line)
  143.                         if match:
  144.                             return match.group('name')
  145.                         continue
  146.                     match
  147.                 
  148.             
  149.             path = None
  150.             match = re.match('pcmC(?P<card>\\d+)D(?P<device>\\d+)(?P<type>\\w)', device)
  151.             if match:
  152.                 path = '/proc/asound/card%s/pcm%s%c/info' % match.groups()
  153.             
  154.             match = re.match('(dsp|adsp|midi|amidi|audio|mixer)(?P<card>\\d+)?', device)
  155.             if match:
  156.                 if not match.group('card'):
  157.                     pass
  158.                 card = 0
  159.                 path = '/proc/asound/card%s/pcm0p/info' % card
  160.             
  161.             if path and posixpath.exists(path):
  162.                 file = open(path, 'r')
  163.                 for line in file.readlines():
  164.                     match = re.match('name: (?P<name>.*)', line)
  165.                     if match:
  166.                         return match.group('name')
  167.                 
  168.             
  169.         
  170.  
  171.     
  172.     def _get_vendor(self):
  173.         if 'RFKILL_NAME' in self._environment:
  174.             return None
  175.         if 'POWER_SUPPLY_MANUFACTURER' in self._environment:
  176.             return self._environment['POWER_SUPPLY_MANUFACTURER']
  177.         if 'sys_vendor' in self._attributes:
  178.             return self._attributes['sys_vendor']
  179.         vendor_path = posixpath.join(self._get_path(), '../vendor_oui')
  180.         if posixpath.exists(vendor_path):
  181.             return open(vendor_path, 'r').read().strip()
  182.  
  183.     
  184.     def items(self):
  185.         return (('path', self._get_path()), ('bus', self._get_bus()), ('category', self._get_category()), ('driver', self._get_driver()), ('product_id', self._get_product_id()), ('vendor_id', self._get_vendor_id()), ('subproduct_id', self._get_subproduct_id()), ('subvendor_id', self._get_subvendor_id()), ('product', self._get_product()), ('vendor', self._get_vendor()), ('attributes', MapRegistry(self._attributes)), ('environment', MapRegistry(self._environment)), ('device', LinkRegistry(self)))
  186.  
  187.  
  188.  
  189. class DmiDeviceRegistry(DeviceRegistry):
  190.     
  191.     def __init__(self, environment, attributes, category):
  192.         super(DmiDeviceRegistry, self).__init__(environment, attributes)
  193.         self._category = category
  194.  
  195.     
  196.     def _get_category(self):
  197.         return self._category
  198.  
  199.     
  200.     def _get_path(self):
  201.         path = super(DmiDeviceRegistry, self)._get_path()
  202.         return posixpath.join(path, self._category.lower())
  203.  
  204.     
  205.     def _get_product(self):
  206.         if self._category == 'CHASSIS':
  207.             type = int(self._attributes['chassis_type'])
  208.             return Dmi.chassis_names[type]
  209.         for name in ('name', 'version'):
  210.             attribute = '%s_%s' % (self._category.lower(), name)
  211.             product = self._attributes.get(attribute)
  212.             if product and product != 'Not Available':
  213.                 return product
  214.         
  215.  
  216.     
  217.     def _get_vendor(self):
  218.         attribute = '%s_vendor' % self._category.lower()
  219.         if attribute in self._attributes:
  220.             return self._attributes[attribute]
  221.  
  222.     _get_vendor = DmiNotAvailable(_get_vendor)
  223.  
  224.  
  225. class UdevRegistry(CommandRegistry):
  226.     '''Registry for udev information.'''
  227.     command = String(default = 'udevadm info --export-db')
  228.     
  229.     def _get_attributes(self, path):
  230.         attributes = { }
  231.         sys_path = '/sys%s' % path
  232.         
  233.         try:
  234.             names = os.listdir(sys_path)
  235.         except OSError:
  236.             return attributes
  237.  
  238.         for name in names:
  239.             name_path = posixpath.join(sys_path, name)
  240.             if name[0] == '.' and name in ('dev', 'uevent') and posixpath.isdir(name_path) or posixpath.islink(name_path):
  241.                 continue
  242.             
  243.             
  244.             try:
  245.                 value = open(name_path, 'r').read().strip()
  246.             except IOError:
  247.                 continue
  248.  
  249.             value = value.split('\n')[0]
  250.             attributes[name] = value
  251.         
  252.         return attributes
  253.  
  254.     
  255.     def _ignore_device(self, device):
  256.         if not device.bus:
  257.             return True
  258.         if not (device.product) and device.product_id is None:
  259.             return True
  260.         if (device.subproduct_id is None or device.subvendor_id is not None or device.subproduct_id is not None) and device.subvendor_id is None:
  261.             return True
  262.         if device.bus != 'dmi' and 'virtual' in device.path.split(posixpath.sep):
  263.             return True
  264.         return False
  265.  
  266.     
  267.     def items(self):
  268.         devices = []
  269.         line_pattern = re.compile('(?P<key>\\w):\\s*(?P<value>.*)')
  270.         multi_pattern = re.compile('(?P<key>\\w+)=(?P<value>.*)')
  271.         for record in str(self).split('\n\n'):
  272.             if not record:
  273.                 continue
  274.             
  275.             path = None
  276.             environment = { }
  277.             for line in record.split('\n'):
  278.                 match = line_pattern.match(line)
  279.                 if not match:
  280.                     raise Exception, 'Device line not supported: %s' % line
  281.                 match
  282.                 key = match.group('key')
  283.                 value = match.group('value')
  284.                 if key == 'P':
  285.                     path = value
  286.                     continue
  287.                 if key == 'E':
  288.                     match = multi_pattern.match(value)
  289.                     if not match:
  290.                         raise Exception, 'Device property not supported: %s' % value
  291.                     match
  292.                     environment[match.group('key')] = match.group('value')
  293.                     continue
  294.             
  295.             environment.setdefault('DEVPATH', path)
  296.             attributes = self._get_attributes(path)
  297.             if path == '/devices/virtual/dmi/id':
  298.                 device = DeviceRegistry(environment, attributes)
  299.                 devices.append(device)
  300.                 for category in ('BIOS', 'BOARD', 'CHASSIS'):
  301.                     device = DmiDeviceRegistry(environment, attributes, category)
  302.                     devices.append(device)
  303.                 
  304.             device = DeviceRegistry(environment, attributes)
  305.             devices.append(device)
  306.         
  307.         return _[1]
  308.  
  309.     items = cache(items)
  310.  
  311. factory = UdevRegistry
  312.